package com.cqie.hospitalescort.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.DTO.EscortDTO;
import com.cqie.hospitalescort.controller.params.EscortParam;
import com.cqie.hospitalescort.entity.EscortInfo;
import com.cqie.hospitalescort.entity.HospitalInfo;
import com.cqie.hospitalescort.entity.UserInfo;
import com.cqie.hospitalescort.enums.RoleEnum;
import com.cqie.hospitalescort.exception.BusinessException;
import com.cqie.hospitalescort.mapper.EscortInfoMapper;
import com.cqie.hospitalescort.mapper.HospitalInfoMapper;
import com.cqie.hospitalescort.mapper.UserInfoMapper;
import com.cqie.hospitalescort.service.EscortInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.hospitalescort.service.file.AliYunService;
import com.cqie.hospitalescort.utils.Asserts;
import com.cqie.hospitalescort.utils.DateUtil;
import com.cqie.hospitalescort.utils.WebSecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 陪诊人员表 服务实现类
 * </p>
 *
 * @author gdy
 * @since 2025-02-18
 */
@Service
public class EscortInfoServiceImpl extends ServiceImpl<EscortInfoMapper, EscortInfo> implements EscortInfoService {

    @Autowired
    private EscortInfoMapper escortInfoMapper;
    @Autowired
    private HospitalInfoMapper hospitalInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private AliYunService aliYunService;

    /**
     * 陪诊员列表
     *
     * @param param
     * @return
     */
    @Override
    public List<EscortInfo> getList(EscortParam param) {
        IPage<EscortInfo> page = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<EscortInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(Objects.nonNull(param.getName()), EscortInfo::getName, param.getName())
                .eq(Objects.nonNull(param.getStatus()), EscortInfo::getEscortStatus, param.getStatus())
                .eq(Objects.nonNull(param.getHospitalId()), EscortInfo::getHospitalId, param.getHospitalId())
                .between(Objects.nonNull(param.getStart()) && Objects.nonNull(param.getEnd()), EscortInfo::getCreateTime, param.getStart(), param.getEnd());
        List<EscortInfo> escortInfos = escortInfoMapper.selectList(page, queryWrapper);
        escortInfos.forEach(escortInfo -> {
            HospitalInfo hospitalInfo = hospitalInfoMapper.selectById(escortInfo.getHospitalId());
            escortInfo.setHospitalInfo(hospitalInfo);
        });

        return escortInfos;
    }

    /**
     * 更新陪诊员信息
     *
     * @param dto
     */
    @Override
    public void save(EscortDTO dto) {
        Asserts.isTrue(Objects.nonNull(dto.getId()), "陪诊员id不能为空！");
        EscortInfo escortInfo = escortInfoMapper.selectById(dto.getId());
        if(Objects.isNull(escortInfo)){
            escortInfo = new EscortInfo();
        }
        escortInfo.buildEscortInfo(dto);
        if (StringUtils.hasText(escortInfo.getPicture())) {
            aliYunService.deleteFilesByUrl(Arrays.asList(escortInfo.getPicture().split(",")));
        }
        escortInfoMapper.updateById(escortInfo);
    }

    /**
     * 删除陪诊员
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        EscortInfo escortInfo = escortInfoMapper.selectById(id);
        if (Objects.nonNull(escortInfo)) {
            UserInfo userInfo = userInfoMapper.selectById(id);
            userInfo.setRole(RoleEnum.USER.getRole());
            userInfoMapper.updateById(userInfo);
            escortInfoMapper.deleteById(id);
        } else {
            throw new BusinessException("陪诊员不存在！");
        }
    }

    /**
     * 查询积分值
     *
     * @param id
     * @return
     */
    @Override
    public Integer getScore(Long id) {
        AuthUser currentUser = WebSecurityUtil.getCurrentUser();

        EscortInfo escortInfo = escortInfoMapper.selectById(currentUser.getId());
        if (Objects.isNull(escortInfo)) {
            throw new BusinessException("陪诊员不存在！");
        }
        return Math.toIntExact(escortInfo.getScore());
    }
}
